<template>
  <j-modal
    :title="title"
    :width="1060"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="门店业绩">
          <div style="width: 100%">
            <a-card v-for="(detail,detailIndex) in detailOptions" size="small" :style="{ marginTop: '10px' }">
              <a-form layout="inline" style="width: 100%;">
                <a-form-item :style="{ width: '20%' }"
                  layout="inline">
                  <span class="ant-input-span">{{ detail.name }}  </span>
                </a-form-item>
                <a-form-item
                  layout="inline">
                  <span class="ant-input-span">业绩：  </span>
                  <a-input-number v-model="detail.orderAmount"/>
                </a-form-item>
                <a-form-item
                  layout="inline">
                  <span class="ant-input-span">主推业绩：  </span>
                  <a-input-number v-model="detail.recommandAmount"/>
                </a-form-item>
                <a-form-item
                  layout="inline">
                  <span class="ant-input-span">新用户：  </span>
                  <a-input-number v-model="detail.consumerCount"/>
                </a-form-item>
                <a-form-item
                  layout="inline">
                  <a href="#" v-if="detailOptions.length > 0" @click="handleDelDetailClick(detailIndex)">删除</a>
                </a-form-item>
              </a-form>
            </a-card>
          </div>
        </a-form-item>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: -520px"
              label="生效时间">
              <a-date-picker :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }" format='YYYY-MM-DD HH:mm:ss'
                             v-decorator="[ 'startTime', validatorRules.startTime]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="失效时间">
              <a-date-picker :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }" format='YYYY-MM-DD HH:mm:ss'
                             v-decorator="[ 'endTime', validatorRules.endTime]"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'

export default {
  name: 'StmKpiStoreAddModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 }
      },
      detailOptions: [],
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        startTime: { rules: [{ required: true, message: '请选择生效时间!' }] },
        endTime: { rules: [{ required: true, message: '请选择失效时间!' }] },
      },
      url: {
        add: '/stm/stmKpiStore/addBatch',
        edit: '/stm/stmKpiStore/edit',
        queryList: '/stm/stmStoreInfo/queryList'
      }
    }
  },
  created() {
  },
  methods: {
    moment,
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.detailOptions = []
      this.getStore()
      this.visible = true
      this.$nextTick(() => {
        //时间格式化
        this.form.setFieldsValue({ startTime: this.model.startTime ? moment(this.model.startTime) : null })
        this.form.setFieldsValue({ endTime: this.model.endTime ? moment(this.model.endTime) : null })
      })

    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values);
          formData.detailList = this.detailOptions;

          if (!formData.detailList) {
            that.confirmLoading = false;
            this.$message.warning('门店业绩为空，请检查！');
            return;
          }
          //时间格式化
          formData.startTime = formData.startTime ? formData.startTime.format('YYYY-MM-DD HH:mm:ss') : null
          formData.endTime = formData.endTime ? formData.endTime.format('YYYY-MM-DD HH:mm:ss') : null

          let index1 = formData.detailList.findIndex(item => {
            if ( item.orderAmount === null  || item.orderAmount === '' || item.orderAmount === undefined || item.orderAmount < 0 ) {
              return true;
            }
            if ( item.recommandAmount === null  || item.recommandAmount === '' || item.recommandAmount === undefined || item.recommandAmount < 0 ) {
              return true;
            }
            if ( item.consumerCount === null  || item.consumerCount === '' || item.consumerCount === undefined || item.consumerCount < 0 ) {
              return true;
            }
          })
          //console.log('index1  => ' + index1);
          if(index1 !== -1) {
            that.confirmLoading = false;
            this.$message.warning('业绩目标内容不符合要求，请检查！');
            return;
          }

          console.log(formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    getStore() {
      this.detailOptions = [];
      getAction(this.url.queryList).then(res=>{
        if(res.success){
          console.log('StoreData => ' + res.result);
          res.result.forEach(item => {
            this.detailOptions.push({ storeId: item.storeId, name: item.name, orderAmount: 0, recommandAmount: 0, consumerCount: 0 })
          })
        }else{
          console.log(res.message);
        }
      });
    },
    handleDelDetailClick (e) {
      this.detailOptions.splice(e, 1)
    },

  }
}
</script>

<style lang="less" scoped>

</style>